<template>
    <div class="manage">
        <div class="manage-header" style="margin-bottom: 10px;">
            <el-button @click="handleAdd()" type="primary">
                +新增
            </el-button>
            <el-form :model="userForm" :inline="true">
                <el-form-item label="请输入姓名">
                    <el-input v-model="userForm.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="common-table">
            <el-table :data="tableData" style="width: 100%" height="90%" stripe>
                <el-table-column prop="name" label="姓名">
                </el-table-column>
                <el-table-column prop="sex" label="性别">
                    <template slot-scope="scope">
                        <span>{{ scope.row.sex == 1 ? '男' : '女' }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="age" label="地址">
                </el-table-column>
                <el-table-column prop="birth" label="出生日期">
                </el-table-column>
                <el-table-column prop="addr" label="地址">
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="handleDel(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pager">
                <el-pagination layout="prev, pager, next" :total="total" @current-change="handlePage">
                </el-pagination>
            </div>
        </div>
        <el-dialog title="新增用户" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form ref="form" :model="form" label-width="80px" :inline="true" :rules="rules">
                <el-form-item label=" 姓名" prop="name">
                    <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="form.age" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择性别">
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" prop="birth">
                    <el-date-picker v-model="form.birth" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="地址" width="100%" prop="addr">
                    <el-input v-model="form.addr" placeholder="请输入地址" style="width: 100%;"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submit()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>


<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getUser, editUser, addUser, deltUser } from '../api'
export default {
    //import 引入的组件需要注入到对象中才能使用
    components: {},
    props: {},
    data() {
        //这里存放数据
        return {
            dialogVisible: false,
            form: {
                name: '',
                age: '',
                sex: '',
                birth: '',
                addr: ''
            },
            rules: {
                name: [
                    {
                        required: true, message: "请输入姓名", trigger: 'blur'
                    }
                ],
                age: [
                    {
                        required: true, message: "请输入年龄", trigger: 'blur'
                    }
                ],
                sex: [
                    {
                        required: true, message: "请选择性别", trigger: 'blur'
                    }
                ],
                birth: [
                    {
                        required: true, message: "请选择出生日期", trigger: 'blur'
                    }
                ],
                addr: [
                    {
                        required: true, message: "请输入地址", trigger: 'blur'
                    }
                ],
            },
            tableData: [

            ],
            modelTape: 0,//0表示新增1弹窗，1表示修改
            total: 0,
            pageData: {
                page: 1,
                limit: 10
            },
            userForm: {
                name: ''
            }

        };
    },
    //计算属性 类似于 data 概念
    computed: {},
    //监控 data 中的数据变化
    watch: {},
    //方法集合
    methods: {

        //提交用户表单
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    //后续对表单数据的处理
                    if (this.modelTape == 0) {
                        addUser(this.form).then(() => {
                            //刷新当前列表
                            this.getList();
                        })
                    } else {
                        editUser(this.form).then(() => {
                            //刷新当前列表
                            this.getList();
                        })
                    }
                    //关闭弹弹窗
                    this.dialogVisible = false;
                    //清空form数据
                    this.$refs.form.resetFields();
                }
            });
        },
        //弹窗关闭前触发的方法
        handleClose() {
            this.$refs.form.resetFields();
            this.dialogVisible = false;
        },
        //点击取消出发的方法
        cancel() {
            this.handleClose();
        },
        // 获取数据公共方法
        getList() {
            getUser({ params: { ...this.userForm, ...this.pageData } }).then(({ data }) => {
                this.tableData = data.list;
                this.total = data.count || 0;
            });
        },
        handleAdd() {
            this.modalType = 0;
            this.dialogVisible = true;
        },
        handleEdit(row) {
            this, this.modelTape = 1;
            this.dialogVisible = true;
            this.form = JSON.parse(JSON.stringify(row)) //深拷贝 如果直接传row会直接修改数据

        },
        handleDel(row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deltUser(row.id).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handlePage(val) {
            this.pageData.page = val;
            this.getList();
        },
        //列表的查询
        onSubmit(){
            this.getList();
        }
    },
    //生命周期 - 创建完成（可以访问当前 this 实例）
    created() {
        this.getList();
    },
    //生命周期 - 挂载完成（可以访问 DOM 元素）
    mounted() {

    },
    beforeCreate() { }, //生命周期 - 创建之前
    beforeMount() { }, //生命周期 - 挂载之前
    beforeUpdate() { }, //生命周期 - 更新之前
    updated() { }, //生命周期 - 更新之后
    beforeDestroy() { }, //生命周期 - 销毁之前
    destroyed() { }, //生命周期 - 销毁完成
    activated() { }, //如果页面有 keep-alive 缓存功能，这个函数会触发
}
</script>
<style lang="less" scoped>
.manage {
    height: 90%;
    .manage-header{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .common-table {
        position: relative;
        height: calc(100% - 62px);

        .pager {
            position: absolute;
            bottom: 0;
            right: 20px;
        }
    }
}
</style>